/************************************************************************/
// Copyright (C) 2016, han_gangbiao. All rights reserved.
// Use of this source code is governed by a GPL-v2 license that can be found in the License file.
// 
// [Time]:      2016-3-20 16:44:19
// [Author]:    han_gangbiao [http://my.oschina.net/jackhen]
// [Info]:      
/************************************************************************/

#include "RPCServer.h"

int main(int argc, char* argv[])
{
    const char* ip = NULL;
    int port = 0;
    if(argc > 2) {
        ip = argv[1];
        port = atoi(argv[2]);
    } else {
        fprintf(stderr, "Usage: %s server_ip server_port\n", argv[0]);
        return -1;
    }
    
    if(port <= 0 || port >= 65536)
    {
        fprintf(stderr, "Usage: %s server_ip server_port\n", argv[0]);
        return -1;
    }
    
    CRpcServerMgr mgr;
    
    smart::ref_ptr<CRPCServer> service = new CRPCServer(&mgr);
    //smart::ref_ptr<CRPCServer> service2 = new CRPCServer(&mgr);
    //multiplex manager
    int ret = service->start_rpc_server(ip, port);
    if(ret < 0) {
        printf("start rpc-server[%s:%d] Failed!\n", ip, port);
        return -1;
    } else {
        printf("start rpc-server[%s:%d] OK! listenfd=[%d]\n", ip, port, ret);
    }
    
    /*ret = service2->start_rpc_server(ip, port + 1);
    if(ret < 0) {
        printf("start rpc-server[%s:%d] Failed!\n", ip, port+1);
        return -1;
    } else {
        printf("start rpc-server[%s:%d] OK! listenfd=[%d]\n", ip, port+1, ret);
    }*/
    
    char c;
	do{
		c = getchar();
		SLEEP_MS(100);
        
        if(c=='k' || c=='K') {
            service->stop_rpc_server();
        }
        if(c=='o' || c=='O') {
            int lfd = service->start_rpc_server(ip, port);
            printf("OK! listenfd=[%d]\n", lfd);
        }

		printf("Press q or Q to exit...\n");
	}while(c!='q' && c!='Q');
    mgr.free();
    printf("exit main......\n");
    return 0;
}